package com.muchfish.reactor.example;

interface FirstCallback {
    void onCompleteFirst(String result);

    void onErrorFirst(Exception e);
}

interface SecondCallback {
    void onCompleteSecond(String result);

    void onErrorSecond(Exception e);
}

interface ThirdCallback {
    void onCompleteThird(String result);

    void onErrorThird(Exception e);
}

class AsyncOperations {
    static void firstOperation(FirstCallback firstCallback) {
        new Thread(() -> {
            try {
                // 模拟异步操作
                Thread.sleep(2000);
                // 操作完成后调用回调函数
                firstCallback.onCompleteFirst("First operation completed");
            } catch (Exception e) {
                // 发生异常时调用错误回调
                firstCallback.onErrorFirst(e);
            }
        }).start();
    }

    static void secondOperation(String input, SecondCallback secondCallback) {
        new Thread(() -> {
            try {
                // 模拟异步操作
                Thread.sleep(2000);
                // 操作完成后调用回调函数
                secondCallback.onCompleteSecond("Second operation completed with input: " + input);
            } catch (Exception e) {
                // 发生异常时调用错误回调
                secondCallback.onErrorSecond(e);
            }
        }).start();
    }

    static void thirdOperation(String input, ThirdCallback thirdCallback) {
        new Thread(() -> {
            try {
                // 模拟异步操作
                Thread.sleep(2000);
                // 操作完成后调用回调函数
                thirdCallback.onCompleteThird("Third operation completed with input: " + input);
            } catch (Exception e) {
                // 发生异常时调用错误回调
                thirdCallback.onErrorThird(e);
            }
        }).start();
    }
}

public class CallbackHellExample {
    public static void main(String[] args) {
        AsyncOperations.firstOperation(new FirstCallback() {
            @Override
            public void onCompleteFirst(String result) {
                System.out.println("First Callback: " + result);

                // 第一次操作完成后调用第二次操作
                AsyncOperations.secondOperation(result, new SecondCallback() {
                    @Override
                    public void onCompleteSecond(String result) {
                        System.out.println("Second Callback: " + result);

                        // 第二次操作完成后调用第三次操作
                        AsyncOperations.thirdOperation(result, new ThirdCallback() {
                            @Override
                            public void onCompleteThird(String result) {
                                System.out.println("Third Callback: " + result);
                            }

                            @Override
                            public void onErrorThird(Exception e) {
                                System.out.println("Error in Third Callback: " + e.getMessage());
                            }
                        });
                    }

                    @Override
                    public void onErrorSecond(Exception e) {
                        System.out.println("Error in Second Callback: " + e.getMessage());
                    }
                });
            }

            @Override
            public void onErrorFirst(Exception e) {
                System.out.println("Error in First Callback: " + e.getMessage());
            }
        });

        // 主线程继续执行其他操作
        System.out.println("Main thread continues...");
    }
}

